import * as robotModel from '../../models/robot';
import { ValidException, EmptyException, SystemException } from '../../helps/exception';

export function* list({ page, number, order, direction }) {
  return yield robotModel.list(page, number, order, direction);
}

export function* add({ content }) {
  // todo 解析content
  if (!content) throw ValidException('文本 不能为空');
  content.replace('\r\n', '\n');
  let contentList = content.split('\n');
  if (!contentList.length === 0) throw ValidException('文本行数必须大于1');
  let dataList = [];
  for (let i in contentList) {
    dataList.push({ code: contentList[i] });
  }
  const robot = yield robotModel.add(dataList);
  if (!robot) throw SystemException('操作失败');
  return { id: robot.id };
}

export function* get({ robot_id }) {
  if (!robot_id) throw ValidException('id 不能为空');
  const robot = yield robotModel.get(robot_id);
  if (!robot) throw EmptyException('无相关信息');
  return robot;
}

export function* put({ robot_id, data }) {
  if (!robot_id) throw ValidException('id 不能为空');
  const b = yield robotModel.put(robot_id, data);
  if (!b) throw SystemException('操作失败');
  return { id: robot_id };
}
